package com.banciyuan.bcywebview.biz.detail.mixweb;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.detail.complex.holder.CommentHolder;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;

import java.util.List;

import de.greenrobot.daoexample.model.DetailComment;
import de.greenrobot.daoexample.model.DetailType;

/**
 * Created by squall1989 on 2017/2/17.
 */

public class MixWebCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<DetailComment> dlist;
    private LayoutInflater inflater;
    private String uid;
    private DetailType detailType;

    public MixWebCommentAdapter(Context mContext, List<DetailComment> dlist, String uid, DetailType detailType) {
        this.uid = uid;
        this.detailType = detailType;
        this.mContext = mContext;
        this.dlist = dlist;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new FooterHolder(inflater.inflate(R.layout.smooth_blank_webmix, null));
        } else {
            return new CommentHolder(inflater.inflate(R.layout.comment_detail_item, null), uid);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0) {
            FooterHolder footerHolder = (FooterHolder) holder;
            if (getItemCount() == 1) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(CommonUtils.getWidthPixels((Activity) mContext), CommonUtils.getHeightPixels((Activity) mContext) - UIUtils.dip2px(108, mContext));
                footerHolder.viewSofa.setLayoutParams(layoutParams);
                footerHolder.viewSofa.setVisibility(View.VISIBLE);
                footerHolder.viewSofa.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((MixWebCommentActivity) mContext).doReply();
                    }
                });
            } else {
                footerHolder.viewSofa.setVisibility(View.GONE);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(60, mContext));
                footerHolder.blank.setLayoutParams(layoutParams);
            }
        } else {
            CommentHolder commentHolder = (CommentHolder) holder;
            commentHolder.setData(position, dlist.get(position), mContext, true, detailType, false);
        }
    }

    @Override
    public int getItemCount() {
        return dlist.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position == dlist.size() ? 0 : 1;
    }

    private class FooterHolder extends RecyclerView.ViewHolder {

        View blank;
        View viewSofa;

        public FooterHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
            viewSofa = itemView.findViewById(R.id.iv_sofa);
        }
    }
}
